<?php
require_once  __DIR__ . '/includes/authenticate.php';
include_once __DIR__ . '/db_bb_connect.php';
?>

<div id="subcontent">
<form method="post" action="" class="hidden-print" enctype="multipart/form-data"> 
  <center><h3>Add/Edit Game Comment</h3></center>
	<div class="form-group">
	  <div class="col-md-offset-3 col-md-6">        
	<select name="season" class="form-control" id="select1">

<?php

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];

	echo "<option id=\"selected-season\" value=\"$setseason\" selected=\"selected\">$setseason is selected</option>";
} else {
	echo "<option id=\"selected-season\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a season</option>";
}

?>	    

<?php

$sql = "SELECT DISTINCT season 
		FROM games
		ORDER BY season";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
				$season = $row['season'];
				echo "<option value=\"$season\">$season</option>";
};
?>

	</select>
	</div>
	</div>

<?php

if (isset($_POST['season'])) {
	$setseason = $_POST['season'];




?>
<br> <br>
<div class="form-group" >
	  <div class="col-md-offset-3 col-md-6">        

<select name="selectedgame" class="form-control" id="select2">

<?php 

if (isset($_POST['season']) && isset($_POST['selectedgame'])) {

	$setseason = $_POST['season'];
	$selectedgame = $_POST['selectedgame'];

	echo "<option id=\"selected-game\" value=\"$selectedgame\" selected=\"selected\">$setseason game $selectedgame selected</option>";         

} else {
	echo "<option id=\"selected-game\" value=\"\" disabled=\"disabled\" selected=\"selected\">Select a game</option>";
}


$sql = "SELECT gamenumber, date, opponent, location
FROM games
WHERE season = '$setseason'
ORDER BY date DESC";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){ 
    $gamenumber = $row['gamenumber'];
    $date = $row['date'];
    $opponent = $row['opponent'];
    $location = $row['location'];

  echo "<option value='$gamenumber'>Game $gamenumber $date ($opponent $location)  </option>";
}

?>
  </select>

</div>
</div>

<?php } 

if (isset($_POST['season']) && isset($_POST['selectedgame'])) {
	$setseason = $_POST['season'];
	$selectedgame = $_POST['selectedgame'];

	$sql4 = "SELECT comment FROM games 
	WHERE season = '$setseason' 
	AND gamenumber = '$selectedgame'";

	$result4=$db->query($sql4);
	$row4=$result4->fetch_assoc();

	$currentcomment = $row4['comment'];

?>
<br><br>
<div class="form-group">
  <label for="usr">Comment</label>
<textarea name="comment" class="form-control" placeholder="Write down comment" rows="10"><?= $currentcomment; ?></textarea>
</div>

<div class="form-group">
  <label for="usr">Youtube Video ID</label>
  <input type="text" class="form-control" id="usr" name="youtubeID">
</div>

<div class="form-group">
  <label for="usr">Game sheet upload</label>
	<input type="file" name="gamesheetfile">
  </div>


<button type="submit" class="btn btn-success btn-lg btn-block submitcommentbutton">Submit Comment</button>


<?php } ?>


</form>
</div>

<script>

$("#select1").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./admin_addeditgamecomment.php",
	  data: postData,
	  success: function(data) {
	  	$("#subcontent").html(data);
  	  }
  }); 
  e.preventDefault();
});

$("#select2").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./admin_addeditgamecomment.php",
	  data: postData,
	  success: function(data) {
	  	$("#subcontent").html(data);
  	  }
  }); 
  e.preventDefault();
});


$(".submitcommentbutton").on("click", function(e) {
    //postData = $(this.form).serialize();
    $.ajax({
	  type: "POST",
	  url: "./includes/handlers/admin_addeditgamecomment_handler.php",
	  //data: postData,
	  data: new FormData(this.form),   
	  contentType: false,
      cache: false,
   	  processData:false,
	  success: function(data) {
	  	//alert(postData);
	  	$("#subcontent").html(data);
  	  },
  	  error: function (xhr, status, error) {
          $("#subcontent").html(xhr.responseText);
        }
  }); 
  e.preventDefault();
});

</script>